<template>
    <div class="block">
        <el-row class="block-item">
            <exSelect label="类型" v-model="type" :dictList="typeList" elstyle="width:120px" size="mini"></exSelect>
            <exInput label="数量" v-model="psize" width="150" size="mini"></exInput>
            <exSwitch label="仅保留首次创新高" v-model="onlyFirst" class="mt5"></exSwitch>
            <el-button type="primary" @click="query" size="mini" style="height:28px;margin-top:4px">查询</el-button>
        </el-row>
        <exPubTable ref="newTable" :cfg.sync="newcfg" :exData="newList" @getDataList="queryList">
            <exColumn label="名称" width="100">
                <template slot-scope="s">
                    <klinePic :code="s.row.f12" :name="s.row.f14"></klinePic>
                </template>
            </exColumn>
        </exPubTable>
    </div>
</template>



<script>
import Vue from "vue";

import klinePic from "../trade/comp/klinePic"

export default {
    components:{
        klinePic,
    },
    data() {
        var that = this;
        return {
            isLoad: false,
            type : "",
            typeList:[{"name":"创业",value:"cy"},{"name":"深证",value:"sz"},{"name":"上证",value:"sh"}],
            psize : 100,
            onlyFirst : true,
            newList:[],
            newcfg:{
                usePage:false,
                showIndex: true,
                btns:[
                    {name:"比对",handle:({row})=>{
                        this.addComp(row);
                    }},
                ],
                columns2: [
                    {prop:"f12",label:"代码",width:80,sortable:true},
                    // {prop:"f14",label:"名称",width:100,sortable:true},
                    {prop:"f2",label:"最新",width:80,sortable:true,align:"right",getColor(s){return that.getRateColor(s.row.f3)}},
                    {prop:"f3",label:"涨跌幅",width:80,sortable:true,align:"right",getColor(s){return that.getRateColor(s.row.f3)},endStr:"%"},
                    {prop:"f4",label:"涨跌额",width:80,sortable:true,align:"right",getColor(s){return that.getRateColor(s.row.f3)}},
                    {prop:"f18",label:"昨收",width:80,sortable:true,align:"right"},
                    {prop:"f17",label:"今开",width:80,sortable:true,align:"right",getColor(s){return that.getColor(s.row.f17,s.row.f18)}},
                    {prop:"f15",label:"最高",width:80,sortable:true,align:"right",getColor(s){return that.getColor(s.row.f15,s.row.f18)}},
                    {prop:"f16",label:"最低",width:80,sortable:true,align:"right",getColor(s){return that.getColor(s.row.f16,s.row.f18)}},
                    {prop:"f7",label:"振幅",width:80,sortable:true,align:"right"},
                    {prop:"f8",label:"换手",width:80,sortable:true,align:"right",endStr:"%"},
                    {prop:"f9",label:"市盈率",width:80,sortable:true,align:"right"},
                    {prop:"f20",label:"总市值",width:100,sortable:true,align:"right",formatter:this.moneyFixedFormat},

                ],
            }
        }
    },
    computed:{
    },
    provide () {
      return {
      }
    },

    mounted() {

    },

    methods: {
        addComp(item){
            window.$vue.addCompItem({code:item.f12,name:item.f14});
        },
        query(){
            if(this.$refs.newTable) this.$refs.newTable.reQuery();
        },
        queryList(){
            this.$axios.get(`http://88.push2.eastmoney.com/api/qt/clist/get?&pn=1&pz=${this.psize}&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f20&fs=m:0+t:6,m:0+t:13,m:0+t:80,m:1+t:2,m:1+t:23&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152&_=1609999355063`)
            .then((res) => {
                if (res.data.data) {
                    this.newList = res.data.data.diff;
                    if(this.type){
                        this.newList = this.newList.filter(item=>{
                            var first = item.f12.substring(0,1);
                            if(this.type=="cy" && first!="3") return false;
                            else if(this.type=="sh" && first!="6") return false;
                            else if(this.type=="sz" && first!="0") return false;

                            return true;
                        })
                    }

                    if(this.onlyFirst){
                        this.getKLineInfo(0)
                    }

                } else {
                    this.$message.error(res.data.message);
                }
            })
        },
        
        getKLineInfo(idx){
            if(idx>=this.newList.length) return;
            var item = this.newList[idx];
            var code = item.f12;
            // if(code!="300898") return;
            var first = code.substring(0,1);
            if(code=="000001"){
                code= "1."+code;
            }else if(first=="0" || first=="3" || first=="1"){
                code = "0."+code;
            }else {
                code = "1."+code;
            }
            this.$axios.get(`http://push2his.eastmoney.com/api/qt/stock/kline/get?secid=${code}&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5%2Cf6&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58%2Cf59%2Cf60%2Cf61&ut=7eea3edcaed734bea9cbfc24409ed989&klt=101&fqt=1&beg=0&end=20500000`)
            .then((res) => {
                if (res.data.data) {
                    var data = res.data.data;
                    var len = data.klines.length;
                    var list = data.klines;
                    if(len>60){
                        list = list.splice(len-60,60)
                    }
                    // if(item.f12=="300999"){
                    //     debugger;
                    // }
                    len = list.length;
                    var secondDay =list[len-1]
                    if(!secondDay) return;
                    var highest = secondDay.split(",")[3]*1;
                    
                    for(var i=len-2;i>=0;i--){ //今天是最高
                        var dayInfo = list[i];
                        var arr = dayInfo.split(",");
                        var dayHighest = arr[3]*1
                        if(dayHighest>highest){ //存在比今天高的,则不是新高
                            this.newList.splice(idx,1);
                            console.info("remove:"+item.f12+item.f14)
                            this.getKLineInfo(idx)
                            return;
                        }
                    }
                    var thirdDay =list[len-2]
                    if(!thirdDay) return;
                    var thirdhighest = thirdDay.split(",")[3]*1;
                    for(var i=len-3;i>=0;i--){//昨天不是最高
                        var dayInfo = list[i];
                        var arr = dayInfo.split(",");
                        var dayHighest = arr[3]*1
                        if(dayHighest>thirdhighest){ //存在比前天高的,则为首次突破
                            this.getKLineInfo(idx+1)
                            return;
                        }
                    }
                    console.info("remove:"+item.f12+item.f14)
                    this.newList.splice(idx,1);
                    this.getKLineInfo(idx)
                } else {
                    this.$message.error(res.data.message);
                }
            })
        },
    }
    
}
</script>
